package com.liuhaoqi.raletiveblog.controller;

import com.liuhaoqi.raletiveblog.entity.User;
import com.liuhaoqi.raletiveblog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @Author liuhaoqi
 * @Date 2020/4/16 13:50
 * @Version 1.0
 */
@Controller
public class LoginController {
    @Autowired
    UserService userService;

    @PostMapping("/adminLogin")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password,
                        RedirectAttributes attributes) {
        User user = userService.checkUser(username, password);
        if (user ==null) {
            attributes.addFlashAttribute("message", "用户名密码错误！");
            return "redirect:/login";
        } else {
            attributes.addFlashAttribute("message", "登录成功！");
            return "admin/index";
        }

    }
}
